import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:helloworld/request/request.dart';

class Register extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _Register();
  }
}

class _Register extends State<Register> {
  bool value = false;
  bool sended = false;
  var _user = TextEditingController();
  var _password = TextEditingController();
  var _code = TextEditingController();
  Widget sendCode() {
    if (!sended) {
      return RaisedButton(
        child: Text("发送验证码"),
        onPressed: () {
          setState(() {
            sended = true;
          });
          Request()
              .post("/sendMail", {'mailAddress': _user.text}).then((res) {});
        },
      );
    } else {
      return RaisedButton(
        child: Text(
          "已发送",
          style: TextStyle(color: Colors.white),
        ),
        onPressed: null,
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.all(16.0),
            child: Text("注册账号", style: TextStyle(fontSize: 30)),
          ),
          Container(
            margin: EdgeInsets.all(16.0),
            child: TextField(
              decoration: InputDecoration(hintText: "请输入邮箱"),
              onChanged: (value) {
                setState(() {
                  _user.text = value;
                });
              },
            ),
          ),
          Container(
            margin: EdgeInsets.all(16.0),
            child: TextField(
              decoration: InputDecoration(hintText: "请输入密码"),
              onChanged: (value) {
                setState(() {
                  _password.text = value;
                });
              },
            ),
          ),
          Row(
            children: <Widget>[
              Expanded(
                flex: 5,
                child: Container(
                  margin: EdgeInsets.all(16.0),
                  child: TextField(
                    decoration: InputDecoration(hintText: "请输入验证码"),
                    onChanged: (value) {
                      setState(() {
                        _code.text = value;
                      });
                    },
                  ),
                ),
              ),
              Expanded(flex: 2, child: sendCode()),
              SizedBox(
                width: 16,
              )
            ],
          ),
          Row(
            children: <Widget>[
              Checkbox(
                onChanged: (bool value) {
                  if (this.value) {
                    setState(() {
                      this.value = false;
                    });
                  } else {
                    setState(() {
                      this.value = true;
                    });
                  }
                },
                value: this.value,
              ),
              Text.rich(TextSpan(children: [
                TextSpan(text: "已阅读并同意"),
                TextSpan(text: "服务协议", style: TextStyle(color: Colors.blue)),
                TextSpan(text: "和"),
                TextSpan(text: "隐私政策", style: TextStyle(color: Colors.blue))
              ]))
            ],
          ),
          Padding(
            padding: const EdgeInsets.all(16.0),
            child: MaterialButton(
              padding: EdgeInsets.all(16.0),
              color: Colors.blueAccent,
              minWidth: double.infinity,
              textColor: Colors.white,
              onPressed: () => {
                Request().post("/confirmCode", {
                  'code': _code.text,
                  'username': _user.text,
                  'userpassword': _password.text
                }).then((res) {
                  print(res.data);
                })
              },
              child: Text("注册"),
            ),
          ),
        ],
      ),
    );
  }
}
